<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	#import {
		position: relative;
		top: 1.4px;
	}
</style>
<div class="page-header">
	<h1>
		闸机管理
		<a href="#modal-gate" class="btn btn-info a-custom add-gate" data-toggle="modal">
			<i class="ace-icon fa fa-plus"></i> 新增闸机
		</a>
	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>设备序列号</th>
				<th>设备类型</th>
				<th>备注</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 新增账号弹窗 -->
<div id="modal-gate" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					闸机管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="gate-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">设备序列号：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="device_sn" placeholder="请填写设备序列号" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">设备类型：</label>
								<div class="col-sm-6">
									<select name="device_is_exit" class="form-control">
										<option value="0">入口</option>
										<option value="1">出口</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">备注：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="device_desc" placeholder="选填。请填写备注" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-gate">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 保存闸机按钮
		var gateForm = $("#gate-form");
		$("#save-gate").click(function(){
			gateForm.submit();
		})
		var submitUrl = "<?=admin_url('school/save_gate_action')?>";
		form_submit(gateForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 添加闸机，清除原数据
		$("body").on("click", ".add-gate", function(){
			gateForm.find("[name=id]").val(undefined)
			gateForm.find("[name=device_sn]").val('');
			gateForm.find("[name=device_desc]").val('');
			gateForm.find("[name=device_sn]").attr('readonly', false);
		})
		// 编辑闸机
		$("body").on("click", ".edit-gate", function(){
			gateForm.find("[name=id]").val($(this).data("id"))
			gateForm.find("[name=device_sn]").val($(this).data("sn"));
			gateForm.find("[name=device_is_exit]").val($(this).data("exit"));
			gateForm.find("[name=device_desc]").val($(this).data("desc"));
			// 编辑不可修改设备序列号
			if($(this).data("id") != undefined) {
				gateForm.find("[name=device_sn]").attr('readonly', true);
			}
		})

		// 闸机表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('school/get_gate_list')?>',
			"columns": [
	            {"data": "device_sn"},
	            {"data": "isExit"},
	            {"data": "device_desc"},
				{"data": "createtime"},
				{"data": function(ret){
					var action = '';
						action += '<a class="edit-gate green mr20" href="#modal-gate" data-toggle="modal" data-id="' + ret.id + '" data-sn="' + ret.device_sn + 
						'" data-exit="' + ret.device_is_exit + '" data-desc="' + ret.device_desc + '"><i class="fa fa-edit"></i> 修改</a>';
						action += '<a class="text-danger delete-gate" href="javascript:" data-id=' + ret.id +  ' data-sn=' + ret.device_sn + '><i class="fa fa-times"></i> 删除</a>';
					return action;
				}}
			]
		});
		// 删除闸机
		$("body").on('click', '.delete-gate', function() {
			var id = $(this).data('id');
			var title = $(this).data('sn');
			var posturl = "<?= admin_url("school/delete_gate_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
	})
</script>